<template>
  <el-dialog class="app-container" :title="title"  width="800px"  :visible.sync="visible" top="5vh" append-to-body>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="关键字查询" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称、编号、型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="stockList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="型号" align="center" prop="model" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="库存数量" align="center" prop="sumQuantity"  >
        <template slot-scope="scope">
          <span v-if="!scope.row.sumQuantity">0</span>
          <span v-if="scope.row.sumQuantity">{{scope.row.sumQuantity}}</span>
        </template>
      </el-table-column>
      <el-table-column label="可用库存量" align="center" prop="sumAvailableQuantity" >
        <template slot-scope="scope">
          <span v-if="!scope.row.sumAvailableQuantity">0</span>
          <span v-if="scope.row.sumAvailableQuantity">{{scope.row.sumAvailableQuantity}}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div  slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleImportProduct" >确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { selectProductQuantityList} from "@/api/module/product";
import {getDicts} from "@/api/system/dict/data";

export default {
  name: "Stock",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存表格数据
      stockList: [],
      // 库存表格数据
      stockQuantityList: [],
      // 弹出层标题
      title: "选择产品",
      // 是否显示弹出层
      open: false,
      visible:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: null,
        productNo: null,
        productId: null,
        model: null,
        quantity: null,
        warehouseId: null,
        warehouseName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      isMultiple:null,
      productModels: [],
      unitOptions:[]
    };
  },
  created() {
    this.getList();
    this.getUnitOptions();
  },
  methods: {
    // 获取单位
    getUnitOptions() {
      getDicts('biz_unit').then(response => {
        this.unitOptions = response.data
      })
    },
    // 显示弹框
    show(isMultiple) {
      this.getList();
      this.visible = true;
      if(isMultiple){
        this.isMultiple=isMultiple;
      }
    },
    /** 查询库存列表 */
    getList() {
      this.loading = true;
      selectProductQuantityList(this.queryParams).then(response => {
        this.stockList = response.rows;
        // 产品类型判断
        let arr = this.stockList;
        arr.forEach(item => {
          item.unit = this.getValue(this.unitOptions, item.unit)
        });
        this.stockList = arr;
        this.total = response.total;
        this.loading = false;
      });
    },
    getValue(arr, item) {
      for (let i = 0; i < arr.length; i++) {
        if (item === arr[i].dictValue) {
          return arr[i].dictLabel
        }
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productName: null,
        productNo: null,
        productId: null,
        model: null,
        quantity: null,
        warehouseId: null,
        warehouseName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.productModels=[];
      if(selection.length>0){
        selection.forEach(v => {
          this.productModels.push({
            id: v.id,
            model:v.model,
            price: v.price,
            productId: v.productId,
            productName: v.productName,
            productNo:v.productNo,
            sumAvailableQuantity: v.sumAvailableQuantity,
            sumQuantity: v.sumQuantity,
            inventoryQuantity: v.sumQuantity,
            unit: v.unit
          })
        })
      }
     /* this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length*/
    },
    handleImportProduct() {
      const productModels = this.productModels;
      if (productModels.length == 0) {
        this.$modal.msgError("请选择产品");
        return;
      }
      this.visible = false;
      this.$emit("showProduct",this.productModels);
    },
  }
};
</script>
